import os
import time
import logging
from pathlib import Path
from src.config import GlobalConfig
from src.review.review_utils import get_last_commit_hash, create_commit_archive, extract_archive, run_bcompare

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('zzl_code.log'),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)


def review():
    # 从GlobalConfig读取配置
    extract_dir = os.path.join(GlobalConfig.get_work_dir(), GlobalConfig.get_config('extract_dir'))
    bcompare_path = GlobalConfig.get_config('bcompare_path')
    
    if not extract_dir or not bcompare_path:
        raise ValueError("配置项 extract_dir 或 bcompare_path 未设置")
    
    # 转换为Path对象
    _work_dir = Path(GlobalConfig.get_work_dir())
    extract_dir = Path(extract_dir)
    bcompare_path = Path(bcompare_path)
    last_commit_hash = get_last_commit_hash(_work_dir)
    if last_commit_hash is None:
        run_bcompare(bcompare_path, _work_dir, extract_dir)
        return "success"
    # Create extract directory if it doesn't exist
    extract_dir.mkdir(parents=True, exist_ok=True)
    
    logger.info(f"Validating git directory: {_work_dir}")

    logger.info(f"Will compare with {extract_dir} using {bcompare_path}")
    
    zip_path = create_commit_archive(_work_dir)
    extract_archive(zip_path, extract_dir)
    run_bcompare(bcompare_path, _work_dir, extract_dir)
    return "success"

